#! /usr/bin/env python

# Alunos:   Eliezer de Souza da Silva  RA: 124065
#           Fernanda Brandao Silva     RA: 060727
#           Michel Silva Fornaciali    RA: 071884
# IA368R: Eduardo Valle - Trabalho 2, Programa 3

import sys
from kmeans import *
import numpy as np
import Image
from scipy.spatial.distance import cdist

def histogram(codebook,points):
    # codebook and points are arrays (from numpy) of integer
    k = codebook.shape[0]
    if(k<2):
        return
    
    hist = np.zeros((k,)) # array inicial zerado com os valores do histograma
    
    #calcula distancia euclidiana entre os pontos da imagem e do codebook 
    distances_to_codewords = cdist(points, codebook, 'euclidean')
    selected = np.argmin(distances_to_codewords, axis=1)
    
    for i in range(k):
        hist[i] = np.where(selected==i)[0].size
        
    return hist
    
# le o codebook    
codebook = np.genfromtxt(sys.argv[1], delimiter=" ")

# le os pixeis da imagem
img = Image.open(sys.stdin)
pixelist = np.array(img.getdata())

# Gera o histograma
hist = histogram(codebook, pixelist)

sys.stdout.write(" ".join(str(int(x)) for x in hist)+"\n")
